﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;
using Yipusoft.ProfessionalTeam.DAL;

public partial class MapInfo_ProfessionalTeam_HelpSandard : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCodeItemList(DeviceClassList, "ZBQC010000");
            BindList();
        }
    }

    void BindList()
    {
        TeamDataContext tdc = new TeamDataContext();
        List<HelpStandard> standards = tdc.HelpStandard.OrderBy(o => o.SetDate).ToList();
        gv.DataSource = standards;
        gv.DataBind();
    }

    private void BindCodeItemList(DropDownList ddl, string parentID)
    {
        ListItem li;
        List<CodeItemInfo> items = CodeItem.GetList("ParentID='" + parentID + "' ORDER BY SeqID");
        foreach (CodeItemInfo item in items)
        {
            li = new ListItem();
            li.Text = "".PadRight(2 * (item.ItemLevel - 1), '　') + item.ItemName;
            li.Value = item.ItemID;
            ddl.Items.Add(li);
            BindCodeItemList(ddl, item.ItemID);
        }
    }

    protected void DeviceClassList_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindStand();
    }

    void bindStand()
    {
        TeamDataContext tdc = new TeamDataContext();
        HelpStandard standard = tdc.HelpStandard.Where(o => o.DeviceClass == DeviceClassList.SelectedValue).FirstOrDefault();
        if (standard != null)
        {
            txtCount.Text = standard.Count.Value.ToString();
        }
        else
        {
            txtCount.Text = "";
        }
    }


    //保存
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        int Count = 0;
        if (!int.TryParse(txtCount.Text, out Count))
        {
            Prompt.Text = "提示：请输入数字！";
            return;
        }
        int Count0 = 0;
        if (txtCount0.Text.Trim() != "")
        {
            if (!int.TryParse(txtCount0.Text, out Count0))
            {
                Prompt.Text = "提示：请输入数字！";
                return;
            }
        }
        TeamDataContext tdc = new TeamDataContext();
        HelpStandard standard = tdc.HelpStandard.Where(o => o.DeviceClass == DeviceClassList.SelectedValue).FirstOrDefault();

        if (standard != null)
        {
            standard.Count = Count;
            standard.SetDate = DateTime.Now;
            standard.SetUserId = User.Identity.Name;
            tdc.SubmitChanges();
            Prompt.Text = "提示：保存成功！";
        }
        else
        {
            standard = new HelpStandard();
            standard.Count = Count;
            standard.DeviceClass = DeviceClassList.SelectedValue;
            standard.Id = Guid.NewGuid();
            standard.SetDate = DateTime.Now;
            standard.SetUserId = User.Identity.Name;
            standard.Count0 = Count0;
            tdc.HelpStandard.InsertOnSubmit(standard);
            tdc.SubmitChanges();
            Prompt.Text = "提示：添加成功！";
        }
        BindList();
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            Label DeviceClassName = e.Row.FindControl("DeviceClassName") as Label;
            HelpStandard standard = e.Row.DataItem as HelpStandard;
            string deviceClass = standard.DeviceClass;
            CodeItemInfo code = CodeItem.GetCodeItem(deviceClass);
            DeviceClassName.Text = code.ItemName;
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            TeamDataContext tdc = new TeamDataContext();
            HelpStandard standard = tdc.HelpStandard.Where(o => o.Id == new Guid(e.CommandArgument.ToString())).FirstOrDefault();
            tdc.HelpStandard.DeleteOnSubmit(standard);
            tdc.SubmitChanges();
            BindList();
        }
    }
}
